package com.example.smartcity_ikun3.ui.home.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.view.MenuItem
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_ikun3.R
import com.example.smartcity_ikun3.logic.util.Util.guide
import com.example.smartcity_ikun3.logic.util.Util.show
import com.example.smartcity_ikun3.ui.App
import com.example.smartcity_ikun3.ui.App.Companion.url
import com.example.smartcity_ikun3.ui.home.HomeViewModel
import com.example.smartcity_ikun3.ui.home.adapter.ReAdapterNews
import com.example.smartcity_ikun3.ui.home.fragment.NewsShowFragment.Companion.idNum
import com.example.smartcity_ikun3.ui.home.model.NewsEntity
import kotlinx.android.synthetic.main.activity_news_show.*

class NewsShowActivity : AppCompatActivity() {

    private val viewModel by lazy {
        ViewModelProviders.of(this).get(HomeViewModel::class.java)
    }


    private lateinit var mTitle: TextView
    private lateinit var mDate: TextView
    private lateinit var mImageView: ImageView
    private lateinit var mContent: TextView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_news_show)
        title = "资讯详情"
        mTitle = findViewById(R.id.newsTitle)
        mDate = findViewById(R.id.newsTime)
        mImageView = findViewById(R.id.newsImageView)
        mContent = findViewById(R.id.newsContent)

        val getTitle = intent.getStringExtra("title")

        getTitle?.let {
            setNewsThis(it)
        }
    }

    private fun setNewsThis(getTitle: String) {
        viewModel.setNews(null)
        viewModel.newsLiveData.observe(this, Observer {
            if (it.code == 200){
                viewModel.newsList.clear()
                viewModel.newsList.addAll(it.rows!!)
            }
            for (i in viewModel.newsList){
                if (i.title == getTitle){
                    mTitle.text = i.title
                    val time = "发布时间: ${i.publishDate}"
                    mDate.text = time
                    guide(url + i.cover.toString(), mImageView)
                    mContent.text = Html.fromHtml(i.content)

                    NewsShowLayout.visibility = View.VISIBLE
                }
            }
        })
    }



    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}